package com.cy.mall.service.wj.impl;

import com.cy.mall.dao.mapper.customer.CustomerMapper;
import com.cy.mall.dao.mapper.wj.WjOrderMapper;
import com.cy.mall.model.Result;
import com.cy.mall.model.ao.wj.WjOrderAddAO;
import com.cy.mall.model.domain.wj.WjOrder;
import com.cy.mall.model.dto.customer.ValidateFaceIdResultDTO;
import com.cy.mall.model.enums.CheckResult;
import com.cy.mall.model.vo.wj.WjOrderDetail;
import com.cy.mall.service.BaseService;
import com.cy.mall.service.wj.WjOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description 万家分期订单 service
 * @Author tanyilin
 * @Date 2019/5/10 16:13
 */
@Service
public class WjOrderServiceImpl extends BaseService implements WjOrderService {

    @Autowired
    private WjOrderMapper  wjOrderMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    @Transactional
    public Result addWjOrder(WjOrderAddAO addAO, Long customerId) {

        Date time = new Date();
        Long wjOrderId = 0L;

        //先验证客户有没有完成人脸识别验证
        ValidateFaceIdResultDTO validateFaceIdResultDTO = customerMapper.selectValidateFaceIdResultDTO(customerId);
        state(validateFaceIdResultDTO != null, "验证客户是否已完成人脸识别，异常");
        state(CheckResult.PASS.equals(validateFaceIdResultDTO.getFaceidCheckResult()), "客户未完成人脸识别验证流程");

        //新增万家分期订单
        WjOrder newWjOrder = new WjOrder();
        BeanUtils.copyProperties(addAO, newWjOrder);
        newWjOrder.setCustomerId(customerId);
        newWjOrder.setCreateTime(time);
        newWjOrder.setUpdateTime(time);
        wjOrderMapper.insertSelective(newWjOrder);
        wjOrderId = newWjOrder.getId();

        return Result.success("新增万家分期订单成功", wjOrderId);
    }

    @Override
    @Transactional
    public void updateWjOrder(WjOrder wjOrder) {
        wjOrder.setUpdateTime(new Date());
        wjOrderMapper.updateByPrimaryKeySelective(wjOrder);
    }

    @Override
    public WjOrderDetail getLastWjOrderDetail(Long customerId) {

        return wjOrderMapper.selectLastWjOrderDetailByCustomerId(customerId);
    }
}
